package domain;


import java.util.ArrayList;
import java.util.List;

public class OrderQueue<T>  {
	boolean isClosed = false;
	List<T> orders = new ArrayList<T>();

	public void addEmail(Object order){
		synchronized (orders) {
			if (!isClosed) {
				orders.add((T) order);
				orders.notify();
			}
		}
	}

	public void close() {
		synchronized (orders) {
			orders.notifyAll();
			isClosed = true;
		}
	}

	public T getEmail() {
		synchronized (orders) {
			if (isClosed && orders.isEmpty())
				return null;
			while (orders.isEmpty() && !isClosed) {
				try {
					orders.wait();
				} catch (InterruptedException e) {
				}
				return null;
			}
			return orders.remove(0);
		}
	}

}